package com.gxa.jjty.admin.service.impl;

import com.gxa.jjty.admin.service.AlarmRecordService;
import com.gxa.jjty.core.pojo.AlarmRecord;
import com.gxa.jjty.core.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.List;

@Service("alarmRecordService")
public class AlarmRecordServiceImpl extends BaseServiceImpl<AlarmRecord> implements AlarmRecordService {
   @Autowired
    Mapper<AlarmRecord> mapper;
    @Override
    public List<AlarmRecord> selectByField(Date from, Date to, AlarmRecord alarmRecord) throws IllegalAccessException {
        Example example = new Example(AlarmRecord.class);
        Example.Criteria criteria = example.createCriteria();
        Class<? extends AlarmRecord> aClass = alarmRecord.getClass();
        Field[] declaredFields = aClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);
            String name = declaredField.getName();
            Object o = declaredField.get(alarmRecord);
            if(o!=null){
                criteria.orLike(name,o.toString());
            }
        }
        if(from!=null&&to!=null){
            criteria.andBetween("regTime",from,to);
        }
        List<AlarmRecord> alarmRecords = mapper.selectByExample(example);
        return  alarmRecords;
    }
}
